package 树的子结构;


public class Solution
{
    public static boolean HasSubtree(TreeNode root1,TreeNode root2)
    {
        //子树同构算法？
        //判断输入
        boolean result=false;
        if (root2!=null&&root1!=null)
        {
            if (root1.val == root2.val)
            {
                result = DoesTree1hasTree2(root1, root2);
            }
            if (!result)
                result =HasSubtree(root1.left, root2);
            if (!result)
                result =HasSubtree(root1.right, root2);
        }
        return result;
    }
    public static boolean DoesTree1hasTree2(TreeNode root1,TreeNode root2)
    {
        //终止条件，子树的结点用尽，到达叶子结点
        if (root2==null)
            return true;
        if (root1==null)
            return false;
        if (root1.val!=root2.val)
            return false;
        return DoesTree1hasTree2(root1.left,root2.left)&&DoesTree1hasTree2(root1.right,root2.right);
    }
}
